<?php

class PropertyValue extends AppModel {

    public $name = 'PropertyValue';
    public $useTable = 'property_value';
    public $primaryKey = 'property_value_id';
    public $hasMany = array(
        'PropertyValueLanguage' => array(
            'className' => 'PropertyValueLanguage',
            'foreignKey' => 'property_value_id',
        )
    );

    /**
     * 
     * @param type $created
     * @param type $options
     */
    public function afterSave($created, $options = array()) {
        parent::afterSave($created, $options);
        App::import('model', 'PropertyValueLanguage');
        App::import('model', 'Language');
        $ob = new PropertyValueLanguage();
        $data = $this->data;
        if ($created) {
            $data['PropertyValueLanguage']['property_value_id'] = $data['PropertyValue']['property_value_id'];
            foreach (Language::getList() as $language) {
                $ob->create();
                $data['PropertyValueLanguage']['language_id'] = $language;
                $data['PropertyValueLanguage']['value'] = $data['PropertyValue']['value'];
                $ob->save($data);
            }
        } else {

            $data['PropertyValueLanguage']['language_id'] = Configure::read('Config.language');
            $data['PropertyValueLanguage']['property_value_id'] = $data['PropertyValue']['property_value_id'];
            $data['PropertyValueLanguage']['value'] = $data['PropertyValue']['value'];

            if ($id = $ob->findPrimaryKey(array('PropertyValueLanguage.language_id' => Configure::read('Config.language'), 'PropertyValueLanguage.property_value_id' => $data['PropertyValue']['property_value_id']))) {
                $ob->create();
                $ob->id = $id;
                $ob->save($data);
            }
        }
    }

    public function beforeDelete($cascade = true) {
        parent::beforeDelete($cascade);
        App::import('model', 'PropertyValueLanguage');
        $ob = new PropertyValueLanguage();
        $ob->deleteAll(array('property_value_id' => $this->id));
    }

    /**
     * 
     */
    public function getValues($id) {
        $result = $this->query("select * from property_value as pv,property_value_language as pvl where pv.property_value_id = pvl.property_value_id AND property_id = " . $id . " AND language_id = '" . Configure::read('Config.language') . "'");
        return $result;
    }

    /**
     * 
     */
    public function findPropertyValueId($propertyId, $value) {
        $sql = "SELECT * FROM property_value as PropertyValue, property_value_language as PropertyValueLanguage";
        $sql .= " WHERE PropertyValue.property_value_id = PropertyValueLanguage.property_value_id";
        $sql .= " AND PropertyValue.property_id = ? AND PropertyValueLanguage.`value` = ?";
        $results = $this->getDataSource()->fetchAll($sql, array($propertyId, $value));
        if (count($results) > 0) {
            return $results[0]['PropertyValue']['property_value_id'];
        } else {
            $this->create();
            $data['PropertyValue']['property_id'] = $propertyId;
            $data['PropertyValue']['status'] = 1;
            $data['PropertyValue']['value'] = $value;
            $this->save($data);
            $propertyValueId = $this->id;
        }
        return $propertyValueId;
    }

    /**
     * 
     */
    public function getValueFromProductAndProperty($product_id, $property_id, $type) {
        $sql = "SELECT PropertyValue.property_value_id ,PropertyValueLanguage.value";
        $sql .= " FROM product_property_value as ProductPropertyValue,property_value as PropertyValue,property_value_language as PropertyValueLanguage";
        $sql .= " WHERE PropertyValue.property_value_id = ProductPropertyValue.property_value_id AND PropertyValueLanguage.property_value_id = PropertyValue.property_value_id";
        $sql .= " AND ProductPropertyValue.product_id = ? AND PropertyValue.property_id = ? AND PropertyValueLanguage.language_id = ?";
        $result = $this->getDataSource()->fetchAll($sql, array($product_id, $property_id, Configure::read('Config.language')));
        
        $property_lang = "SELECT * FROM `property_language`, property
            WHERE property_language.property_id = property.property_id
            AND property_language.language_id = ?
            AND property_language.property_id = ?
            ";
        $result_property_lang = $this->getDataSource()->fetchAll($property_lang, array( Configure::read('Config.language'),$property_id));
        
        $return = array(
            'property_value_id' => 0,
            'property_value_language' => "",
            'property_language' =>"",
            'other' => 0
        );
        //
        if ($type == 1) {
            if (count($result) > 0) {
                $return = array(
                    'property_value_id' => $result[0]['PropertyValue']['property_value_id'],
                    'property_value_language' => $result[0]['PropertyValueLanguage']['value'],
                    'property_language' => $result_property_lang[0]['property_language']['name'],
                    'oder' => $result_property_lang[0]['property']['oder']
                );
            }
        }
        //
        if ($type == 2) {
            $return = array('property_value' => array(),'property_value_language' => array(), 'property_language'=>$result_property_lang[0]['property_language']['name'],'oder' => $result_property_lang[0]['property']['oder']);
            foreach ($result as $value) {
                $return['property_value'][] = $value['PropertyValue']['property_value_id'];
                $return['property_value_language'][] = $value['PropertyValueLanguage']['value'];
            }
        }
        //
        if ($type == 3) {
            if (count($result) > 0) {
                $return = array(
                    'property_value_id' => $result[0]['PropertyValue']['property_value_id'],
                    'property_value_language' => $result[0]['PropertyValueLanguage']['value'],
                    'property_language' => $result_property_lang[0]['property_language']['name'],
                    'oder' => $result_property_lang[0]['property']['oder']
                );
            }
        }
        return $return;
    }
    
    /**
     * 
     */
    public function getValueFromProperty($propertyId){
        $sql = "SELECT * ";
        $sql .= " FROM property_value as PropertyValue,property_value_language as PropertyValueLanguage";
        $sql .= " WHERE PropertyValueLanguage.property_value_id = PropertyValue.property_value_id";
        $sql .= " AND PropertyValue.property_id = ? AND PropertyValueLanguage.language_id = ?";
        $result = $this->getDataSource()->fetchAll($sql, array( $propertyId, Configure::read('Config.language')));
        return $result;
    }

}
